import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class WindowTwo extends JFrame {
    JButton button, button1, button2, button3;
    static JTextField jTextField;
    JTextField jTextField1;
    JComboBox<String> comboBox1;
    JComboBox<String> comboBox2;
    JComboBox<String> comboBox3;
    WindowTwoListener windowTwoListener;
    WindowThreeListener windowThreeListener;
    XSSFWorkbook workbook;

    Box boxOne, boxTwo, boxThree, boxFour, boxFive, boxSix, boxSeven, boxAll;
    JLabel jLabel1, jLabel2, jLabel3, jLabel4 , jLabel5;
    JPanel jPanel;

    WindowTwo() {
        init();
        setTitle("实验报告小助手");
        setSize(500, 700);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    void init() {
        FileInputStream fileInput = null;
        try {
            fileInput = new FileInputStream(new File("F:\\java\\work\\JavaHomeWork\\src\\stu.xlsx"));
            workbook = new XSSFWorkbook(fileInput);
        } catch (IOException e) {
            System.out.println("读取错误！");
        }

        boxAll = Box.createVerticalBox();
        boxOne = Box.createHorizontalBox();
        boxTwo = Box.createHorizontalBox();
        boxThree = Box.createHorizontalBox();
        boxFour = Box.createHorizontalBox();
        boxFive = Box.createHorizontalBox();
        boxSix = Box.createHorizontalBox();
        boxSeven = Box.createHorizontalBox();

        jLabel1 = new JLabel("实验报告小助手");
        jLabel1.setFont(new Font("仿宋", Font.BOLD, 20));
        boxOne.add(jLabel1);

        //选择班级
        XSSFSheet sheet1 = workbook.getSheetAt(0);
        XSSFSheet sheet2 = workbook.getSheetAt(1);
        String[] list1 = {sheet1.getSheetName(), sheet2.getSheetName()};
        comboBox1 = new JComboBox<String>(list1);
        comboBox1.setBackground(Color.white);
        comboBox1.setPreferredSize(new Dimension(220, 22));
        comboBox1.setSelectedIndex(0);
        jLabel2 = new JLabel("班级：");
        jLabel2.setFont(new Font("仿宋", Font.BOLD, 18));
        boxTwo.add(jLabel2);
        boxTwo.add(comboBox1);

        //选择实验
        String[] list2 = new String[6];
        int j = 5;
        for(int i  = 0; i<6 ;i++){
            list2[i]= String.valueOf(sheet1.getRow(2).getCell(j));
            j++;
        }
        comboBox2 = new JComboBox<String>(list2);
        comboBox2.setBackground(Color.white);
        comboBox2.setPreferredSize(new Dimension(220, 22));
        comboBox2.setSelectedIndex(0);
        jLabel2 = new JLabel("实验：");
        jLabel2.setFont(new Font("仿宋", Font.BOLD, 18));
        boxThree.add(jLabel2);
        boxThree.add(comboBox2);

        button = new JButton("确认");
        button.setBackground(Color.white);//按钮样式设置
        button.setFocusPainted(false);
        boxThree.add(new JLabel("    "));
        boxThree.add(button);

        //选择姓名
        String[] name = new String[3];
        int k = 4;
        for(int i = 0 ; i < 3; i++){
            name[i] = String.valueOf(workbook.getSheetAt(0).getRow(k).getCell(1));
            k++;
        }
        jLabel3 = new JLabel("姓名：");
        jLabel3.setFont(new Font("仿宋", Font.BOLD, 18));
        comboBox3 = new JComboBox<>(name);
        comboBox3.setBackground(Color.white);
        comboBox3.setPreferredSize(new Dimension(220, 22));
        comboBox3.setSelectedIndex(0);
        boxFour.add(jLabel3);
        boxFour.add(comboBox3);

        jLabel4 = new JLabel("学号：");
        jLabel4.setFont(new Font("仿宋", Font.BOLD, 18));
        jTextField = new JTextField();
        jTextField.setPreferredSize(new Dimension(200, 22));
        jTextField.setText(String.valueOf(workbook.getSheetAt(0).getRow(4).getCell(3)));
        boxFive.add(jLabel4);
        boxFive.add(jTextField);

        jLabel5 = new JLabel("分数：");
        jLabel5.setFont(new Font("仿宋", Font.BOLD, 18));
        jTextField1 = new JTextField();
        jTextField1.setPreferredSize(new Dimension(200, 22));
        boxSix.add(jLabel5);
        boxSix.add(jTextField1);

        button3 = new JButton("上一个");
        button3.setBackground(Color.white);//按钮样式设置
        button3.setFocusPainted(false);
        button3.setEnabled(false);
        boxSeven.add(new JLabel("    "));
        boxSeven.add(button3);
        button1 = new JButton("储存");
        button1.setBackground(Color.white);//按钮样式设置
        button1.setFocusPainted(false);
        boxSeven.add(new JLabel("    "));
        boxSeven.add(button1);
        button2 = new JButton("下一个");
        button2.setBackground(Color.white);//按钮样式设置
        button2.setFocusPainted(false);
        boxSeven.add(new JLabel("    "));
        boxSeven.add(button2);

        boxAll.add(Box.createVerticalStrut(25));//间距
        boxAll.add(boxOne);
        boxAll.add(Box.createVerticalStrut(30));
        boxAll.add(boxTwo);
        boxAll.add(Box.createVerticalStrut(30));
        boxAll.add(boxThree);
        boxAll.add(Box.createVerticalStrut(30));
        boxAll.add(boxFour);
        boxAll.add(Box.createVerticalStrut(30));
        boxAll.add(boxFive);
        boxAll.add(Box.createVerticalStrut(30));
        boxAll.add(boxSix);
        boxAll.add(Box.createVerticalStrut(30));
        boxAll.add(boxSeven);

        windowTwoListener =new WindowTwoListener();
        windowTwoListener.setWindowTwo(this);
        button.addActionListener(windowTwoListener);
        button2.addActionListener(windowTwoListener);
        button3.addActionListener(windowTwoListener);
        comboBox3.addActionListener(windowTwoListener);

        windowThreeListener = new WindowThreeListener();
        windowThreeListener.setWindowTwo(this);
        button1.addActionListener(windowThreeListener);
        button2.addActionListener(windowThreeListener);
        button3.addActionListener(windowThreeListener);
        comboBox2.addActionListener(windowThreeListener);
        comboBox3.addActionListener(windowThreeListener);

        jPanel = new JPanel();
        jPanel.add(boxAll);

        this.setLayout(new BorderLayout());
        this.add(jPanel, BorderLayout.NORTH);
    }
}
